//顶部导航
$.ajax({
    type: 'get',
    url: '../../json/allsort1.json',
    async: 'true',
    success: function (data) {
        var topArr = data.topNav;
        for (var i = 0; i < topArr.length; i++) {
            var node = $(`<li><a href="">
                ${topArr[i].title}
            </a></li>`);
            node.appendTo(".nav #licontent");
        }
    }

})
//左边商品列表
$.ajax({
    type: 'get',
    url: '../../json/allsort1.json',
    async: true,
    success: function (data) {
        var sideArr = data.sideNav;
        for (var i = 0; i < sideArr.length; i++) {
            var node = $(`<li class = "licontent">
                <a href = "/index.html" class = "title">
                    ${sideArr[i].title}
                </a>
                </li>`);
            node.appendTo(".nav #ul_level12");
        }

    }
})
$.ajax({
    type: 'get',
    url: '../../json/search.json',
    async: true,
    success: function (data) {
        var searchArr = data.search;
        for (var i = 0; i < searchArr.length; i++) {
            var node = $(`<a href="" class="curr">${searchArr[i].name}</a>`);
            node.appendTo(".search #hotWord");

        }
    }
})

function Cart() {
    //属性
    this.$xq = $('.gou');
    this.init();
    this.addEvent();

}
Cart.prototype = {
    constructor: Cart,
    addEvent() {
        this.$xq.on('click',function(){
            location.href = 'http://localhost:8090/huijiayou/dist/pages/xq.html'
        })
    },
    init() {
        //获取cookie
        let cookie_str = $.cookie('carts') ? $.cookie('carts') : '';
        let cookie_obj = convertStrToObj(cookie_str);
        //遍历
        for (let key in cookie_obj) {
            let good = cookie_obj[key];
            $('.trolley').append(
                `
                <tr id="tr_242568_242568" class="curr">
                            <td width="60">
                                 <input type="checkbox" class="id" name="" data-good-id="${key}" checked="">
                            </td>
                            <td width="560" colspan="2">
                            <a>
                                <img src="${good.src}" alt="" width="83" height="83" class ="img">
                                    <p>
                                        <font></font>
                                        <b class="name">${good.name}</b>
                                    </p>
                                    <strong>颜色：共同<br>款式：共同</strong>
                                </a>
                                
                            </td>
                            <td width="125"><span class="">${good.price}<i style="width: auto;">特价</i></span></td>
                            <td width="150">
                                <label>
                                    <i style="cursor:pointer" id="i_sub_242568_242568" class="curr">-</i>
                                    <input id="txtSkuNum_242568_242568" type="tel" value="${good.num}">
                                    <i style="cursor:pointer" id="i_add_242568_242568" >+</i>
                                </label>
                                <br>
                              
                            </td>
                            <td width="105"><span class="s11">${2021 * good.num}</span></td>
                            <td width="102"><a style="cursor:pointer" class="del" >删除</a></td>
                        </tr>      
                `
            )
        }
        //-
        $('#i_sub_242568_242568').each(function () {
            $(this).click(function () {
                // let id = $(this).parent().parent().prev().prev().prev().children().attr('data-good-id');
                let id = $('.id').attr('data-good-id');
                let cookie_str = $.cookie('carts') ? $.cookie('carts') : '';
                let cookie_obj = convertStrToObj(cookie_str);
                if (cookie_obj[id].num > 1) {
                    cookie_obj[id].num--;
                }
                $.cookie('carts', JSON.stringify(cookie_obj), { expires: 3, path: '/' });
                $(this).next().val(cookie_obj[id].num);
                $('.s11').text(2021 * cookie_obj[id].num);

            })
        })
        //+
        $('#i_add_242568_242568').each(function () {
            $(this).click(function () {
                // let id = $(this).parent().parent().prev().prev().prev().children().attr('data-good-id');
                let id = $('.id').attr('data-good-id');
                let cookie_str = $.cookie('carts') ? $.cookie('carts') : '';
                let cookie_obj = convertStrToObj(cookie_str);
                cookie_obj[id].num++;
                $.cookie('carts', JSON.stringify(cookie_obj), { expires: 3, path: '/' });
                $(this).prev().val(cookie_obj[id].num);
                $('.s11').text(cookie_obj[id].num * 2021);
            })
        })
        //改数字
        $('#txtSkuNum_242568_242568').each(function () {
            $(this).blur(function () {
                // let id = $(this).parent().parent().prev().prev().prev().children().attr('data-good-id');
                let id = $('.id').attr('data-good-id');
                // alert(id);
                let cookie_str = $.cookie('carts') ? $.cookie('carts') : '';
                let cookie_obj = convertStrToObj(cookie_str);
                if (/^\d+$/.test($(this).val())){
                    cookie_obj[id].num = $(this).val();
                } else {
                    cookie_obj[id].num = 1;
                }
                $.cookie('carts', JSON.stringify(cookie_obj), { expires: 3, path: '/' });
                $(this).val(cookie_obj[id].num);
                $('.s11').text(cookie_obj[id].num * 2021);


            })
        })


        $('.del').each(function () {
            $(this).click(function () {
                // let id = $(this).parent().prev().prev().prev().prev().prev().children().attr('data-good-id');
                let id = $('.id').attr('data-good-id');
                console.log(id);
                let cookie_str = $.cookie('carts') ? $.cookie('carts') : '';
                let cookie_obj = convertStrToObj(cookie_str);
                delete cookie_obj[id];
                $.cookie('carts', JSON.stringify(cookie_obj), { expires: 3, path: '/' });
                // $(this).parent().prev().prev().prev().prev().prev().children().remove();
                $('#tr_242568_242568').remove();
            })
        })
    }
}
new Cart();